import 'package:get/get.dart';
import 'package:flutter/material.dart';

import '../../../controllers/auth_controller.dart';
import '../../../routes/app_routes.dart';
import '../../../services/storage/local_storage_service.dart';

/// 个人资料控制器
class ProfileController extends GetxController {
  // 注入依赖
  final AuthController _authController = Get.find<AuthController>();
  final LocalStorageService _storageService = Get.find<LocalStorageService>();

  // 用户信息
  final RxString _userName = ''.obs;
  String get userName => _userName.value;

  final RxString _userEmail = ''.obs;
  String get userEmail => _userEmail.value;

  final RxString _userAvatarUrl = ''.obs;
  String get userAvatarUrl => _userAvatarUrl.value;
  
  final RxString _userRole = ''.obs;
  String get userRole => _userRole.value;
  
  final RxString _registrationDate = ''.obs;
  String get registrationDate => _registrationDate.value;

  // 其他设置
  final RxBool _notificationsEnabled = true.obs;
  bool get notificationsEnabled => _notificationsEnabled.value;

  // 加载状态
  final RxBool _isLoading = false.obs;
  bool get isLoading => _isLoading.value;

  @override
  void onInit() {
    super.onInit();
    _loadUserProfile();
    _loadSettings();
  }

  /// 加载用户资料
  void _loadUserProfile() {
    _isLoading.value = true;
    // 从 AuthController 获取当前登录用户信息
    _userName.value = _authController.userName;
    _userEmail.value = _authController.userEmail;
    _userAvatarUrl.value = _authController.userAvatarUrl;
    _userRole.value = _authController.userRoleDisplay;
    _registrationDate.value = _authController.registrationDate;
    _isLoading.value = false;
  }

  /// 加载本地设置
  void _loadSettings() {
    _notificationsEnabled.value = _storageService.read<bool>('notifications_enabled') ?? true;
  }

  /// 导航到编辑个人资料页面
  void navigateToEditProfile() {
    // Get.toNamed(AppRoutes.EDIT_PROFILE);
    Get.snackbar('提示', '编辑个人资料功能正在开发中');
  }

  /// 导航到账户设置页面
  void navigateToAccountSettings() {
    Get.toNamed(AppRoutes.SETTINGS_ACCOUNT);
  }
  
  /// 导航到通知设置页面
  void navigateToNotificationSettings() {
    Get.toNamed(AppRoutes.SETTINGS_NOTIFICATION);
  }
  
  /// 导航到隐私设置页面
  void navigateToPrivacySettings() {
    Get.toNamed(AppRoutes.SETTINGS_PRIVACY);
  }

  /// 导航到关于页面
  void navigateToAbout() {
    Get.toNamed(AppRoutes.SETTINGS_ABOUT);
  }
  
  /// 切换通知设置
  void toggleNotifications(bool value) {
    _notificationsEnabled.value = value;
    _storageService.write('notifications_enabled', value);
    Get.snackbar(
      '设置已更新',
      '通知设置已${value ? '开启' : '关闭'}',
      snackPosition: SnackPosition.BOTTOM,
    );
  }

  /// 退出登录
  Future<void> logout() async {
    final confirm = await Get.dialog<bool>(
      AlertDialog(
        title: const Text('确认退出'),
        content: const Text('您确定要退出登录吗？'),
        actions: [
          TextButton(
            onPressed: () => Get.back(result: false),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () => Get.back(result: true),
            child: const Text('确定', style: TextStyle(color: Colors.red)),
          ),
        ],
      ),
    );

    if (confirm == true) {
      await _authController.logout();
    }
  }
} 